﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace NCKH.Admin
{
    public partial class Nhaxuatban_Sua : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                if (Request.QueryString["pid"] != null)
                {
                    int nhaxuatbanID = Convert.ToInt32(Request.QueryString["pid"]);
                    hienNXB(nhaxuatbanID);
                }
        }
        private void hienNXB(int nhaxuatbanID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlSelect = "SELECT MaNXB, TenNXB, SDT, Fax, Diachi  FROM NHAXUATBAN WHERE MaNXB='" + nhaxuatbanID + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            txtTenNXB.Text = rd["TenNXB"].ToString();
                            txtSDT.Text = rd["SDT"].ToString();
                            txtFax.Text = rd["Fax"].ToString();
                            txtDiachi.Text = rd["Diachi"].ToString();
                            btnCapnhat.CommandArgument = nhaxuatbanID.ToString();
                        }
                        rd.Close();
                    }
                    Cnn.Close();
                }
            }
        }
        protected void btnBoqua_Click(object sender, EventArgs e)
        {
            txtTenNXB.Text
                = txtSDT.Text
                = txtFax.Text
                = txtDiachi.Text = string.Empty;
            txtTenNXB.Focus();
        }

        protected void btnCapnhat_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("nhaxuatban_Sua", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaNXB", btnCapnhat.CommandArgument);
                    Cmd.Parameters.AddWithValue("@TenNXB", txtTenNXB.Text);
                    Cmd.Parameters.AddWithValue("@SDT", txtSDT.Text);
                    Cmd.Parameters.AddWithValue("@Fax", txtFax.Text);
                    Cmd.Parameters.AddWithValue("@Diachi", txtDiachi.Text);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
            Response.Redirect("Nhaxuatban.aspx");
        }
    }
}